package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author qzh
 * @create 2020-01-31 2020/1/31
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findUserByUsername(String username) {
        User user = userDao.findUserByUsername(username);
        return user;
    }

    @Override
    public List<Map<String, Object>> findIsFineCount() {

       return userDao.findIsFineCount();

    }


    //2.添加用户信息
    public void add(User user,Integer[]roleIds) {
        userDao.add(user);
        Integer user_id = user.getId();
        if(roleIds!=null&&roleIds.length>0){
            for (Integer role_id : roleIds) {
                userDao.setUR(user_id,role_id);
            }
        }
    }

    //3.添加用户角色信息
    public void addRole(Integer[] checkitemIds, Integer user_id) {
        if(checkitemIds!=null&&checkitemIds.length>0){
            for (Integer role_id : checkitemIds) {
                userDao.setId(user_id,role_id);
            }
        }

    }

    //4.用户管理的分页查询
    //1.角色的分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //1.获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //2.获取每页显示的记录数
        Integer pageSize = queryPageBean.getPageSize();
        //3.获取查询条件
        String queryString = queryPageBean.getQueryString();
        //4.基于mybatis，使用分页插件助手
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.findByCondition(queryString);
        long total = page.getTotal();
        List<User> rows = page.getResult();
        PageResult pageResult = new PageResult(total,rows);
        return pageResult;

    }

    //1.回显角色信息
    public List<Integer> findByUId(Integer id) {
        return userDao.findByUId(id);


    }


}
